<?php
//*** Add funtion to set file
$filename = "Users.txt";
$loggedUser = null;
$key = "AITSolutions"

function attemptLogin($username, $password)//Returns a Username if the user is found and the password matches, if user is found and password doesnot match or no user is found return is null.
	{
	 global $filename;
	 global $loggedUser;
	 $array = file($filename); 
	 foreach($array as $value)
		{
		$user = explode(":",$value);
			if($user[0]==$username && $user[1]==$password)
			{
					$loggedUser=$user[0];
					return $user[0];
			}
		}
		return null;
	}

function attemptLoginSecure($username, $password)//Returns a Username if the user is found and the password matches, if user is found and password doesnot match or no user is found return is null.
	{
	 global $filename;
	 global $loggedUser;
	 $array = file($filename); 
	 foreach($array as $value)
		{
		$user = explode(":",$value);
			if($user[0]==$username && decrypt($user[1])==$password)
			{
					$loggedUser=$user[0];
					return $user[0];
			}
		}
		return null;
	}

function setSourceFile($newFileLoc)//Sets the new source file for the user details.
	{
    $this->filename = $newFileLoc;
	}

function getSourceFile()//returns the filename of the source file for user details. Defualt is "Users.txt".
	{
    return $this->filename;
	}	
  
function getLoggedUser()
	{
	return $this->$loggedUser;
	}
 
function addUser($username, $password)//adds the specifed username and password to the user details.
	{
    global $filename;
    $fh = fopen($filename,"a");
    $data = "\n".$username.":".$password.":";
    fwrite($fh,$data);
    fclose($fh); 
	}

function addUserSecure($username, $password)//adds the specifed username and password to the user details.
	{
    global $filename;
    $fh = fopen($filename,"a");
    $data = "\n".$username.":".encrypt($password).":";
    fwrite($fh,$data);
    fclose($fh); 
	}

function encrypt($string)
	{
	global $key;
	return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
	}
	
function decrypt($string)
	{
	global $key;
	return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
	}
	
function close()
	{
	global $loggedUser;
	$loggedUser = null;
	}	
?>